package cn.com.chec.en.controller.front;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import cn.com.chec.en.dao.ChannelInfoDao;
import cn.com.chec.en.dao.NewsInfoDao;
import cn.com.chec.en.dao.PageLinkDao;
import cn.com.chec.en.dao.TagInfoDao;
import cn.com.chec.en.model.domain.ChannelInfo;
import cn.com.chec.en.model.domain.NewsInfo;
import cn.com.chec.en.model.domain.QueryNewsObject;
import cn.com.chec.en.model.domain.TagInfo;
import cn.com.chec.en.model.dto.NewsDTO;
import cn.com.chec.en.util.ContactUtils;
import cn.com.chec.en.util.PageBean;

@Controller
public class BusinessFrontController {
	protected static final Logger logger = Logger
			.getLogger(BusinessFrontController.class);

	@Resource
	private PageLinkDao pageLinkDao;

	@Resource
	private ChannelInfoDao channelInfoDao;

	@Resource
	private NewsInfoDao newsInfoDao;

	@Resource
	private TagInfoDao tagInfoDao;

	@Resource
	private Map<String, Object> environment;

	@RequestMapping(value = { "/business" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	public ModelAndView index(
			@RequestParam(value = "chId", defaultValue = "2", required = false) String chId,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("business");

		map.put("pageLinkMap", this.pageLinkDao.getAllAsMap());

		map.putAll(ContactUtils.getContactMap());

		int chIdInt = 2;
		if ((StringUtils.isNotBlank(chId)) && (chId.matches("\\d+"))) {
			chIdInt = Integer.valueOf(chId).intValue();
		}

		List<ChannelInfo> channelList = this.channelInfoDao
				.getSubListByParentIdLanguageId(1, chIdInt, 0, 2147483647);
		map.put("channelList", channelList);

		ChannelInfo navChannelInfo = this.channelInfoDao
				.getChannelByChannelId(chIdInt);
		map.put("navChannelInfo", navChannelInfo);

		map.putAll(this.environment);
		mav.addAllObjects(map);

		return mav;
	}

	@RequestMapping(value = { "/business/children/{chId}" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	public ModelAndView children(@PathVariable String chId,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("busiChild");

		map.put("pageLinkMap", this.pageLinkDao.getAllAsMap());

		map.putAll(ContactUtils.getContactMap());

		int chIdInt = 2;
		if ((StringUtils.isNotBlank(chId)) && (chId.matches("\\d+"))) {
			chIdInt = Integer.valueOf(chId).intValue();
		}

		ChannelInfo clInfo = this.channelInfoDao.getChannelByChannelId(chIdInt);
		ChannelInfo pInfo = this.channelInfoDao.getChannelByChannelId(clInfo
				.getChannelParentId());
		List<ChannelInfo> channelList = null;
		if (clInfo.getHasChild() == 1) {
			channelList = this.channelInfoDao.getSubListByParentIdLanguageId(1,
					chIdInt, 0, 2147483647);
		} else {
			channelList = this.channelInfoDao.getSubListByParentIdLanguageId(1,
					2, 0, 2147483647);
		}

		map.put("channelInfo", clInfo);
		map.put("pChannelInfo", pInfo);
		map.put("channelList", channelList);

		map.putAll(this.environment);
		mav.addAllObjects(map);

		return mav;
	}

	@RequestMapping(value = { "/business/busiNews/{chId}" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	public ModelAndView list(
			@PathVariable String chId,
			@RequestParam(value = "curPage", defaultValue = "1", required = false) String curPage,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("biList");

		Map<String, String> pageLinkMap = this.pageLinkDao.getAllAsMap();
		logger.info("[NewsFrontController] pageLinkMap=" + pageLinkMap);
		map.put("pageLinkMap", pageLinkMap);

		map.putAll(ContactUtils.getContactMap());

		PageBean pageBean = new PageBean();
		int curPageInt = (StringUtils.isNotBlank(curPage))
				&& (curPage.matches("\\d+")) ? Integer.parseInt(curPage) : 1;
		pageBean.setCurPage(curPageInt);

		int chIdInt = 2;
		if ((StringUtils.isNotBlank(chId)) && (chId.matches("\\d+"))) {
			chIdInt = Integer.valueOf(chId).intValue();
		}

		List<ChannelInfo> channelList = null;
		ChannelInfo clInfo = this.channelInfoDao.getChannelByChannelId(chIdInt);
		ChannelInfo pInfo = this.channelInfoDao.getChannelByChannelId(clInfo
				.getChannelParentId());
		if (clInfo.getHasChild() == 1) {
			channelList = this.channelInfoDao.getSubListByParentIdLanguageId(1,
					chIdInt, 0, 2147483647);
		} else if (clInfo.getChannelParentId() != 2) {
			channelList = this.channelInfoDao.getSubListByParentIdLanguageId(1,
					clInfo.getChannelParentId(), 0, 2147483647);
		} else {
			channelList = this.channelInfoDao.getSubListByParentIdLanguageId(1,
					2, 0, 2147483647);
		}
		map.put("channelInfo", clInfo);
		map.put("pChannelInfo", pInfo);
		map.put("channelList", channelList);

		StringBuilder tags = new StringBuilder();
		List<TagInfo> tagList = this.tagInfoDao.getTagListByChannelId(chIdInt);
		if ((tagList != null) && (tagList.size() > 0)) {
			tags.append("<div class=\"txt_tag\">");
			for (TagInfo tag : tagList) {
				tags.append("<a href=\"" + request.getContextPath() + "/tag/"
						+ tag.getId() + ",\">" + tag.getTag()
						+ "</a>&nbsp;&nbsp;");
			}
			tags.append("</div>");
		}
		map.put("tags", tags);

		QueryNewsObject queryObject = new QueryNewsObject();
		queryObject.setLanguageId(1);
		queryObject.setIsShowInList(1);
		queryObject.setStart(pageBean.getStart());
		queryObject.setNum(pageBean.getRowsPerPage());
		List<NewsDTO> newsList = this.newsInfoDao
				.getNewsListByQueryPChannelIdDistinct(queryObject, chIdInt);
		pageBean.setMaxRowCount(this.newsInfoDao
				.getNewsCountByQueryPChannelIdDistinct(queryObject, chIdInt));
		pageBean.setMaxPage();
		pageBean.setPageNoList();

		map.put("newsInfoDTOList", newsList);
		map.put("pageBean", pageBean);

		map.putAll(this.environment);
		mav.addAllObjects(map);

		return mav;
	}

	@RequestMapping(value = { "/business/dev" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	public ModelAndView dev(
			@RequestParam(value = "chId", defaultValue = "", required = false) String chId,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("development");

		Map<String, String> pageLinkMap = this.pageLinkDao.getAllAsMap();
		logger.info("[NewsFrontController] pageLinkMap=" + pageLinkMap);
		map.put("pageLinkMap", pageLinkMap);

		map.putAll(ContactUtils.getContactMap());

		int defaultChannelId = 0;
		List<ChannelInfo> channelList = this.channelInfoDao
				.getSubListByParentIdLanguageId(1, 3, 0, 2147483647);
		if ((channelList != null) && (channelList.size() > 0)) {
			defaultChannelId = ((ChannelInfo) channelList.get(0))
					.getChannelId();
		}
		map.put("channelList", channelList);

		int chIdInt = (StringUtils.isNotBlank(chId)) && (chId.matches("\\d+")) ? Integer
				.valueOf(chId).intValue() : defaultChannelId;
		NewsDTO dto = this.newsInfoDao.getNewsDTOByChannelId(chIdInt);
		ChannelInfo channelInfo = dto.getRel() != null ? dto.getRel()
				.getChannelnfo()
				: dto.getRel().getChannelnfo() == null ? new ChannelInfo()
						: new ChannelInfo();
		NewsInfo newsInfo = dto.getNewsInfo() != null ? dto.getNewsInfo()
				: new NewsInfo();

		map.put("channelInfo", channelInfo);
		map.put("newsInfo", newsInfo);

		map.putAll(this.environment);
		mav.addAllObjects(map);

		return mav;
	}
}